package com.ruoyi.web.controller.business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtFollowRecord;
import com.ruoyi.business.service.IHurtFollowRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 随访记录Controller
 *
 * @author ruoyi
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/business/followRecord")
public class HurtFollowRecordController extends BaseController
{
    @Autowired
    private IHurtFollowRecordService hurtFollowRecordService;

    /**
     * 查询随访记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:followRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtFollowRecord hurtFollowRecord)
    {
        startPage();
        List<HurtFollowRecord> list = hurtFollowRecordService.selectHurtFollowRecordList(hurtFollowRecord);
        return getDataTable(list);
    }

    /**
     * 导出随访记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:followRecord:export')")
    @Log(title = "随访记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtFollowRecord hurtFollowRecord)
    {
        List<HurtFollowRecord> list = hurtFollowRecordService.selectHurtFollowRecordList(hurtFollowRecord);
        ExcelUtil<HurtFollowRecord> util = new ExcelUtil<HurtFollowRecord>(HurtFollowRecord.class);
        util.exportExcel(response, list, "随访记录数据");
    }

    /**
     * 获取随访记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:followRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtFollowRecordService.selectHurtFollowRecordById(id));
    }

    /**
     * 新增随访记录
     */
    @PreAuthorize("@ss.hasPermi('business:followRecord:add')")
    @Log(title = "随访记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtFollowRecord hurtFollowRecord)
    {
        return toAjax(hurtFollowRecordService.insertHurtFollowRecord(hurtFollowRecord));
    }

    /**
     * 修改随访记录
     */
    @PreAuthorize("@ss.hasPermi('business:followRecord:edit')")
    @Log(title = "随访记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtFollowRecord hurtFollowRecord)
    {
        return toAjax(hurtFollowRecordService.updateHurtFollowRecord(hurtFollowRecord));
    }

    /**
     * 删除随访记录
     */
    @PreAuthorize("@ss.hasPermi('business:followRecord:remove')")
    @Log(title = "随访记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtFollowRecordService.deleteHurtFollowRecordByIds(ids));
    }

    /**
     * 获取下次随访记录
     * @return
     */
    @GetMapping("/getNextFollow")
    public AjaxResult getNextFollow(){
        Long patientId = SecurityUtils.getUserId();
        Map<String, Object> nextFollow = hurtFollowRecordService.getNextFollow(patientId);
        if (nextFollow != null) {
            return AjaxResult.success(nextFollow);
        }else {
            return AjaxResult.success(new HashMap<>());
        }
    }

    /**
     * 获取历史随访记录
     * @return
     */
    @GetMapping("/getHistoryFollowList")
    public TableDataInfo getHistoryFollowList(){
        Long patientId = SecurityUtils.getUserId();
        startPage();
        List<Map<String, Object>> list = hurtFollowRecordService.getHistoryFollowList(patientId);
        return getDataTable(list);
    }

    /**
     * 医师获取随访记录
     * @return
     */
    @GetMapping("/getPatientFollowList")
    public TableDataInfo getPatientFollowList(@RequestParam Long patientId){
        System.out.println("patientId = " + patientId);
        startPage();
        List<Map<String, Object>> list = hurtFollowRecordService.getPatientFollowList(patientId);
        return getDataTable(list);
    }
}
